package main

import (
	"crypto/tls"
	"fmt"
	"io"
	"log"
	"net/http"
	"time"
)

func main() {
	// 强制使用HTTP/2
	conf := &tls.Config{
		PreferServerCipherSuites: true,
		MinVersion:               tls.VersionTLS12,
	}
	transport := &http.Transport{
		TLSClientConfig: conf,
		// 强制使用HTTP/2
		ForceAttemptHTTP2: true,
	}
	client := &http.Client{
		Transport: transport,
		Timeout:   5 * time.Second, // 设置超时时间
	}

	url := "https://www.deepl.com/translator"
	//url := "https://www.feishu.cn/"
	//url := "https://yiyan.baidu.com/"
	for i := 0; i < 10; i++ {
		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			log.Fatalf("Failed to create request: %v", err)
		}

		resp, err := client.Do(req)
		if err != nil {
			log.Printf("Request %d failed: %v", i+1, err)
			continue
		}
		defer func(Body io.ReadCloser) {
			err := Body.Close()
			if err != nil {

			}
		}(resp.Body)

		// 读取响应体
		body, err := io.ReadAll(resp.Body)
		if err != nil {
			log.Printf("Failed to read response body for request %d: %v", i+1, err)
			continue
		}

		// 判断请求是否成功
		if resp.StatusCode >= 200 && resp.StatusCode < 300 {
			fmt.Printf("Request %d succeeded. Status: %s\n", i+1, resp.Status)
			// 如果只需要判断请求是否成功，可以在这里退出循环或进行其他操作
		} else {
			fmt.Printf("Request %d failed with status code: %d\n", i+1, resp.StatusCode)
		}

		// 打印部分响应内容
		fmt.Printf("Response for request %d (first 100 bytes):\n%s\n", i+1, string(body[:100]))

		// 等待一段时间再发起下一次请求
		time.Sleep(1 * time.Second)
	}
}
